package cn.ryh.designpatterndemo.chainofresponsibilitypattern;

/**
 * @ClassName AbstractHandler
 * @Description TODO
 * @Author RaoYH
 * @Date 2019/4/25
 */
public abstract class AbstractHandler {

    private AbstractHandler nextHandler;

    //每个处理者都有一个处理级别
    protected abstract Level getHandlerLevel();

    // 每个处理者都必须实现处理任务
    protected abstract Response echo(Request request);

    public final Response handleMessage(Request request) {
        Response response = null;
        //判断是否是自己的处理级别
        if (this.getHandlerLevel().getLevel().equals(request.getLevel().getLevel())) {
            response = this.echo(request);
        } else { //不属于自己的处理级别
            //判断是否有下一个处理者
            if (this.nextHandler != null) {
                response = this.nextHandler.handleMessage(request);
            } else {
                System.out.println("没有适当的处理者，业务自行处理");
            }
        }
        return response;
    }

    public AbstractHandler getNextHandler() {
        return nextHandler;
    }
    //设置下一个处理者是谁
    public void setNextHandler(AbstractHandler nextHandler) {
        this.nextHandler = nextHandler;
    }
}
